package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyAdsDao;
import ex.com.jm.food.entity.Ads;
import ex.com.jm.food.entity.PicStyle;

public class CompanyAdsDaoImpl extends HibernateDaoSupport implements
		CompanyAdsDao {

	public CompanyAdsDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Ads> getAds(final int initPage,final int pageRow,final String comId,
			final String adName,final Integer status,final String s_date,final String e_date,
			final String order,final String sort) {
		// TODO Auto-generated method stub
		Object ads=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from Ads ads where ads.comId='"+comId+"' ";
				if(adName!=null&&!adName.equals("")){
					hql+=" and ads.adName like '%"+adName+"%'";
				}
				if(status!=null&&!status.equals("")){
					hql+=" and ads.status="+status;
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and ads.created>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and ads.created<='"+e_date+"'";
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by "+order+" "+sort;
				}else{
					hql+=" order by ads.created desc";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Ads>) ads;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int adsCount(final String comId,final String adName,final Integer status,
			final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {
		@Override
		public Object doInHibernate(Session session)
				throws HibernateException, SQLException {
			// TODO Auto-generated method stub
			String hql=" from Ads ads where ads.comId='"+comId+"' ";
			if(adName!=null&&!adName.equals("")){
				hql+=" and ads.adName like '%"+adName+"%'";
			}
			if(status!=null&&!status.equals("")){
				hql+=" and ads.status="+status;
			}
			if(s_date!=null&&!s_date.equals("")){
				hql+=" and ads.created>='"+s_date+"'";
			}
			if(e_date!=null&&!e_date.equals("")){
				hql+=" and ads.created<='"+e_date+"'";
			}
			Query query=session.createQuery(hql);
			return query.list().size();
		}
	});
		return count;
	}

	@Override
	public Object saveAds(Ads ads) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(ads);
	}

	@Override
	public void updateAds(Ads ads) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(ads);
	}

	@Override
	public Ads editAds(String adId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Ads.class, adId);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PicStyle> getPicStyles() {
		// TODO Auto-generated method stub
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from PicStyle where styleType=1";
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<PicStyle>)object;
	}

	@Override
	public void delAds(String adId) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Ads.class, adId));
	}

}
